package com.alibaba.algo.cooperation.controller;

import com.alibaba.algo.cooperation.constants.GlobalResultStatus;
import com.alibaba.algo.cooperation.entity.BaseResult;
import com.alibaba.algo.cooperation.entity.dto.req.GetDingTalkUserReqDTO;
import com.alibaba.algo.cooperation.entity.dto.res.DingTalkUserResDTO;
import com.alibaba.algo.cooperation.service.DingTalkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 *
 */
@RestController
@Slf4j
@RequestMapping(value = "/usercenter/thirdparty/access/dingtalk")
public class DingTalkController {

    @Autowired
    private DingTalkService dingTalkService;


    @ResponseBody
	@PostMapping("/dinguser")
	public BaseResult<DingTalkUserResDTO> getDingUser(@RequestBody GetDingTalkUserReqDTO dto) {
        // 参数判空
        if(ObjectUtils.isEmpty(dto) || ObjectUtils.isEmpty(dto.getCode()) || ObjectUtils.isEmpty(dto.getAppkey()) || ObjectUtils.isEmpty(dto.getAppSecret())){
            if(null == dto){
                log.error("DingTalkController getDingUser param dto is null.");
            }else{
                log.error("DingTalkController getDingUser params are illegal. code:{},appKey:{},appSecret:{}",dto.getCode(),dto.getAppkey(),dto.getAppSecret());
            }
            return BaseResult.buildError(GlobalResultStatus.PARAM_ERROR.getCode(), GlobalResultStatus.PARAM_ERROR.getMsg());
        }
        //调用接口获取钉钉用户信息
        DingTalkUserResDTO dingTalkUserResDTO = dingTalkService.getUserGetResponseNew(dto.getCode(),dto.getAppkey(),dto.getAppSecret(),dto.getBaseUrl());
        log.info("DingTalkController getDingUser,dingTalkUserResDTO:{}",dingTalkUserResDTO);
		return BaseResult.buildSuccess(dingTalkUserResDTO);
	}

}
